package com.ruoyi.tea.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.tea.cache.SeeCache;
import com.ruoyi.tea.domain.*;
import com.ruoyi.tea.service.ITeaFollowRecoringService;
import com.ruoyi.tea.service.ITeaFollowRoomService;
import com.ruoyi.tea.service.ITeaUserService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * 收藏API
 *
 * @author wangfeng
 * @time 2022年08月06日00:39:32
 */
@Controller
@RequestMapping("/wx/api/follow")
public class FollowApiController extends BaseController {

    @Autowired
    private ITeaUserService iTeaUserService;

    @Autowired
    private ITeaFollowRoomService iTeaFollowRoomService;

    @Autowired
    private ITeaFollowRecoringService iTeaFollowRecoringService;

    @RequestMapping("/live/list")
    @ResponseBody
    public TableDataInfo liveFollowList(@RequestBody TeaFollowRoom teaFollowRoom) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaFollowRoom.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(500);
            tableDataInfo.setMsg("未登录");
            return tableDataInfo;
        }
        startPage();
        List<TeaRoom> list = iTeaFollowRoomService.selectTeaFollowRoomApiList(teaFollowRoom);
        return getDataTable(list);
    }

    @RequestMapping("/recording/list")
    @ResponseBody
    public TableDataInfo recordingFollowList(@RequestBody TeaFollowRecoring teaFollowRecoring) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaFollowRecoring
                .getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(500);
            tableDataInfo.setMsg("未登录");
            return tableDataInfo;
        }
        startPage();
        List<TeaRecoring> list = iTeaFollowRecoringService.selectTeaFollowRecoringApiList(teaFollowRecoring);
        return getDataTable(list);
    }

    @RequestMapping("/live")
    @ResponseBody
    public AjaxResult liveFollow(@RequestBody TeaFollowRoom teaFollowRoom) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaFollowRoom.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        teaFollowRoom.setUserName(teaUserList.get(0).getName());
        teaFollowRoom.setNickName(teaUserList.get(0).getNickName());
        teaFollowRoom.setPhone(teaUserList.get(0).getPhone());
        teaFollowRoom.setUserCode(teaUserList.get(0).getUserCode());
        teaFollowRoom.setAnchorId(teaFollowRoom.getAnchorId());
        iTeaFollowRoomService.insertTeaFollowRoom(teaFollowRoom);
        return AjaxResult.success();
    }

    @RequestMapping("/nolive")
    @ResponseBody
    public AjaxResult noLiveFollow(@RequestBody TeaFollowRoom teaFollowRoom) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaFollowRoom.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        TeaFollowRoom tmp = new TeaFollowRoom();
        tmp.setOpenId(teaFollowRoom.getOpenId());
        tmp.setRoomCode(teaFollowRoom.getRoomCode());
        tmp.setStatus(0);
        List<TeaFollowRoom> list = iTeaFollowRoomService.selectTeaFollowRoomList(tmp);
        if (list == null || list.size() == 0) {
            return AjaxResult.error("未关注");
        }
        for(TeaFollowRoom follow:list){
            tmp = new TeaFollowRoom();
            tmp.setId(follow.getId());
            tmp.setStatus(1);
            iTeaFollowRoomService.updateTeaFollowRoom(tmp);
        }

        return AjaxResult.success();
    }

    @RequestMapping("/recording")
    @ResponseBody
    public AjaxResult mediaFollow(@RequestBody TeaFollowRecoring teaFollowRecoring) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaFollowRecoring.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        teaFollowRecoring.setUserName(teaUserList.get(0).getName());
        teaFollowRecoring.setNickName(teaUserList.get(0).getNickName());
        teaFollowRecoring.setPhone(teaUserList.get(0).getPhone());
        teaFollowRecoring.setUserCode(teaUserList.get(0).getUserCode());
        iTeaFollowRecoringService.insertTeaFollowRecoring(teaFollowRecoring);
        return AjaxResult.success();
    }

    @RequestMapping("/norecording")
    @ResponseBody
    public AjaxResult noMediaFollow(@RequestBody TeaFollowRecoring teaFollowRecoring) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaFollowRecoring.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        TeaFollowRecoring tmp = new TeaFollowRecoring();
        tmp.setOpenId(teaFollowRecoring.getOpenId());
        tmp.setRecordingCode(teaFollowRecoring.getRecordingCode());
        tmp.setStatus(0);
        List<TeaFollowRecoring> list = iTeaFollowRecoringService.selectTeaFollowRecoringList(tmp);
        if (list == null || list.size() == 0) {
            return AjaxResult.error("未收藏");
        }
        for(TeaFollowRecoring follow:list){
            tmp = new TeaFollowRecoring();
            tmp.setId(follow.getId());
            tmp.setStatus(1);
            iTeaFollowRecoringService.updateTeaFollowRecoring(tmp);
        }

        return AjaxResult.success();
    }

}
